package com.ztc.ZtcDriver.dao;

import com.aqgj.util.Pager;
import com.ztc.ZtcDriver.bean.ZtcDriver;

import java.math.BigInteger;
import java.util.List;

import org.springframework.stereotype.Repository;
import com.aqgj.util.BaseDaoImpl;

@Repository("ZtcDriverDao")
public class ZtcDriverDao extends BaseDaoImpl<ZtcDriver> {


    /**
     * 根据司机id更新司机状态.
     *
     * @param did    司机did
     * @param status 状态
     * @return 更新条数
     */
    public int updateZcDriverStatus(String did, String status) {
        String hql = "update ZcDriver d set d.status=? where d.id=?";
        return this.createQuery(hql, new String[]{status, did}).executeUpdate();
    }

    public String save(ZtcDriver ztcDriver){
        this.currentSession().save(ztcDriver);
        return ztcDriver.getId();
    }

    public String update(ZtcDriver ztcDriver) {
        this.currentSession().merge(ztcDriver);
        return ztcDriver.getId();
    }

    //根据公司id获取旗下车或车方车(运营公司获取旗下车已审核的车和本公司车)
    public Pager<ZtcDriver> getDriverListByTransportId(String transportId, Integer pageSize, Integer pageNumber, Integer transportType, String search,String status) {
        Pager<ZtcDriver> pager = new Pager<>();
        pager.setPageSize(pageSize);
        pager.setCurrentPage(pageNumber);
        String sql = "select z.* from ztc_driver z where z.transport_id='" + transportId + "'";
        //平台公司
        if(transportType==0){
            if("1".equals(status)){//运营列表
                sql = "select z.* from ztc_driver z,ztc_transport c where z.transport_id=c.id and z.`status`<>2 and z.status<>5 ";
            }
            if("2".equals(status)){//待审核列表
                sql = "select z.* from ztc_driver z,ztc_transport c where z.transport_id=c.id and z.`status`=2 ";
            }
            if (search != null && !"".equals(search)) {
                sql +=" and (z.name like '%" + search + "%' or z.driver_card like '%"+search+"%' or z.phone like '%" + search + "%' or c.full_name like '%" + search + "%')";
            }
        //运营公司
        }else if (transportType == 3) {
            if("1".equals(status)){//运营列表
                sql = "select z.* from ztc_driver z,ztc_transport c where z.transport_id=c.id and (z.transport_id='" + transportId + "' or (c.parent_id='" + transportId + "' and z.`status`<>2 and z.status<>5 )) ";
            }
            if("2".equals(status)){//待审核列表
                sql = "select z.* from ztc_driver z,ztc_transport c where z.transport_id=c.id and (z.transport_id='" + transportId + "' or c.parent_id='" + transportId + "') and z.`status`=2 ";
            }
            if (search != null && !"".equals(search)) {
                sql += "and (z.name like '%" + search + "%' or z.driver_card like '%"+search+"%' or z.phone like '%" + search + "%' or c.full_name like '%" + search + "%')";
            }
        } else{//非运营（车方）
            if("2".equals(status)){
                sql = "select z.* from ztc_driver z where z.transport_id='" + transportId + "' and z.status=2 ";
                if (search != null) {
                    sql = "select z.* from ztc_driver z,ztc_transport c where z.transport_id=c.id and z.transport_id='" + transportId + "' and z.status=2 and (z.name like '%" + search + "%' or z.driver_card like '%"+search+"%' or z.phone like '%" + search + "%' or c.full_name like '%" + search + "%')";
                }
            }else{
                if (search != null && !"".equals(search)) {
                    sql = "select z.* from ztc_driver z,ztc_transport c where z.transport_id=c.id and z.transport_id='" + transportId + "' and (z.name like '%" + search + "%' or z.phone like '%" + search + "%' or c.full_name like '%" + search + "%')";
                }
            }
        }
        pager.setRecordTotal(this.currentSession().createNativeQuery(sql,ZtcDriver.class).getResultList().size());
        sql += " limit ?,?";
        pager.setContent(this.currentSession().createNativeQuery(sql, ZtcDriver.class).setParameter(1, pager.getStart()).setParameter(2, pageSize).getResultList());
        return pager;
    }

    public List<ZtcDriver> getDriverListByTransportId(String id, Integer type) {
        String sql ="select z.* from ztc_driver z where z.transport_id='" + id + "' and z.`status`=1 " ;
        if (type == 3) {//运营公司
           sql = "select z.* from ztc_driver z,ztc_transport c where z.transport_id=c.id and (z.transport_id='" + id + "' or c.parent_id='" + id + "') and z.`status`=1";
         }
        return this.getHibernateTemplate().getSessionFactory().getCurrentSession().createNativeQuery(sql, ZtcDriver.class).getResultList();
    }

    //获取所有驾驶证证件到期且未被冻结的司机
    public List<ZtcDriver> getExpireDriverList() {
        String sql ="select z.* from ztc_driver z where z.`status`=1 and z.end_validate_time<curdate()" ;
        return this.currentSession().createNativeQuery(sql, ZtcDriver.class).getResultList();
    }
}